package com.solar.passwordsafe;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class PassEdit extends Activity {
	
	private static String TAG = "PassEdit";
		
    private EditText descriptionText;
    private EditText passwordText;
    private EditText usernameText;
    private EditText websiteText;
    private EditText noteText;
    private Long RowId;
    private Db4oHelper db;
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pass_edit);
		
		db=new Db4oHelper(this,getString(R.string.database));

		descriptionText=(EditText)findViewById(R.id.description);
		passwordText=(EditText)findViewById(R.id.password);
		usernameText=(EditText)findViewById(R.id.username);
		websiteText=(EditText)findViewById(R.id.website);
		noteText=(EditText)findViewById(R.id.note);
		
		Button confirmBtn=(Button)findViewById(R.id.save);
		
		try {
			Bundle bundle=getIntent().getExtras();
			RowId=bundle.getLong(PassList.KEY_ID);
		} catch (Exception e) {
			// TODO: handle exception
			RowId=null;
		}
		
		populateFields();
		
		confirmBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if(descriptionText.getText().toString().trim().length()==0) {
					NotificationManager nm=(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
					nm.notify(0, new Notification(R.drawable.icon, getString(R.string.notify_blank_desc),100 ));
					return;
				}
				
				saveState();
				setResult(RESULT_OK);
				finish();
			}
		});
				
	}
	
	private void saveState() {
		PassEntry pe =null;
		if(RowId!=null) {
			pe=db.getPasswordById(RowId);
		}
		else {
			pe=new PassEntry();
		}
		
		pe.setDescription(descriptionText.getText().toString());
		pe.setNote(noteText.getText().toString());
		pe.setPassword(passwordText.getText().toString());
		pe.setUserName(usernameText.getText().toString());
		pe.setWebSite(websiteText.getText().toString());
		
		db.storePassword(pe);
	}
	
	private void populateFields() {
		if(RowId!=null) {
			PassEntry pe=db.getPasswordById(RowId);
			if(pe.getId()>-1) {
				descriptionText.setText(pe.getDescription());
				passwordText.setText(pe.getPassword());
				usernameText.setText(pe.getUserName());
				websiteText.setText(pe.getWebSite());
				noteText.setText(pe.getNote());
			}
		}
	}
	
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		
		db.close();
		db=null;
	}

	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if(db==null) {
			db=new Db4oHelper(this,getString(R.string.database));
		}
		populateFields();
	}
    

}
